<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高德地图 PyQt6 集成</title>
    <style>
        body, html, #mapContainer {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
    <!-- 引入高德地图 JS API -->
    <script src="https://webapi.amap.com/maps?v=2.0&key=608d75903d29ad471362f8c58c550daf"></script>
</head>
<body>
    <div id="mapContainer"></div>
    
    <script>
        // 初始化地图
        let map = new AMap.Map('mapContainer', {
            zoom: 10,
            center: [116.397428, 39.90923], // 默认中心为北京
            resizeEnable: true
        });
        
        // 标记点图层
        let markersLayer = new AMap.LayerGroup();
        map.add(markersLayer);
        
        // 路线图层
        let routeLayer = new AMap.LayerGroup();
        map.add(routeLayer);
        
        // 搜索地点
        window.searchLocation = function(keywords) {
            AMap.plugin('AMap.PlaceSearch', function() {
                let placeSearch = new AMap.PlaceSearch({
                    pageSize: 10,
                    pageIndex: 1
                });
                
                placeSearch.search(keywords, function(status, result) {
                    if (status === 'complete' && result.info === 'OK') {
                        let pois = result.poiList.pois;
                        showSearchResults(pois);
                        
                        // 发送结果到Python
                        window.pybridge.receiveSearchResults(JSON.stringify(pois));
                    }
                });
            });
        };
        
        // 显示搜索结果
        function showSearchResults(pois) {
            // 清空标记点
            markersLayer.clearLayers();
            
            pois.forEach(poi => {
                // 添加标记点
                addMarker(poi.location, poi.name);
            });
        };
        
        // 添加标记点
        function addMarker(position, name) {
            let marker = new AMap.Marker({
                position: position,
                title: name,
                icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png'
            });
            
            markersLayer.add(marker);
        };
        
        // 定位到指定位置，将中文函数名改为英文
        window.goToLocation = function(position) {
            let pos = position.split(',').map(Number);
            map.setZoomAndCenter(15, pos);
        };
        
        // 获取当前位置
        window.getCurrentLocation = function() {
            AMap.plugin('AMap.Geolocation', function() {
                let geolocation = new AMap.Geolocation({
                    enableHighAccuracy: true,
                    timeout: 10000
                });
                
                map.addControl(geolocation);
                geolocation.getCurrentPosition(function(status, result) {
                    if (status === 'complete') {
                        let location = result.position.lng + ',' + result.position.lat;
                        window.pybridge.receiveLocation(location);
                    }
                });
            });
        };
        
        // 计算路线
        window.calculateRoute = function(origin, destination) {
            AMap.plugin('AMap.Driving', function() {
                let driving = new AMap.Driving({
                    map: map,
                    panel: null
                });
                
                driving.search(origin, destination, function(status, result) {
                    if (status === 'complete') {
                        // 清空路线图层
                        routeLayer.clearLayers();
                        
                        // 绘制路线
                        if (result.routes && result.routes.length > 0) {
                            let route = result.routes[0];
                            drawRoute(route);
                        }
                    }
                });
            });
        };
        
        // 绘制路线
        function drawRoute(route) {
            // 绘制路线
            let path = [];
            route.steps.forEach(step => {
                step.path.forEach(point => {
                    path.push([point.lng, point.lat]);
                });
            });
            
            let polyline = new AMap.Polyline({
                path: path,
                strokeColor: "#3366FF",
                strokeWeight: 6,
                strokeOpacity: 0.8,
                lineJoin: 'round',
                lineCap: 'round',
                zIndex: 50
            });
            
            // 添加起点和终点标记
            let startMarker = new AMap.Marker({
                position: path[0],
                title: '起点',
                icon: 'https://webapi.amap.com/theme/v1.3/markers/n/start.png'
            });
            
            let endMarker = new AMap.Marker({
                position: path[path.length - 1],
                title: '终点',
                icon: 'https://webapi.amap.com/theme/v1.3/markers/n/end.png'
            });
            
            routeLayer.add(polyline);
            routeLayer.add(startMarker);
            routeLayer.add(endMarker);
            
            // 调整地图视野以显示完整路线
            map.setFitView([polyline]);
        };
    </script>
</body>
</html>